﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAO;
using BUS;

namespace GUI
{
    public partial class frmLapThucDon : Form
    {
        private BindingSource bs = new BindingSource();
        //private BindingSource DSChon = new BindingSource();
        List<string> dd = new List<string>();
        public frmLapThucDon(List<string> td)
        {
            InitializeComponent();
            dd = td;
            
           
        }
        List<CHITIET_THUCDON> arrChiTiet = new List<CHITIET_THUCDON>();
        public void DanhSachMonAn_HienTai(int mathucdon)
        {
            gvDSMonAn.Rows.Clear();
            LapThucDonBus busTD = new LapThucDonBus();
            arrChiTiet = busTD.DanhSachThucDon(mathucdon);
            for (int i = 0; i < arrChiTiet.Count; i++)
            {
                DataGridViewRow _newRow = (DataGridViewRow)gvDSChon.Rows[0].Clone();
               // _newRow.Cells[0].Value = arrChiTiet[i].MaThucDon;
                _newRow.Cells[0].Value = arrChiTiet[i].MaMonAn;
                _newRow.Cells[1].Value = arrChiTiet[i].MONAN.TenMonAn;
                _newRow.Cells[2].Value = arrChiTiet[i].SoPhanAn;
                gvDSChon.Rows.Add(_newRow);

            }
        }

        public void LayDS_MonAn()
        {
           LapThucDonBus busTD = new LapThucDonBus();
           List<MonAnPublic> MonAn = new List<MonAnPublic>();
           
           MonAn = busTD.LayDS_MonAn();
           if (dd[3] == "u")
           {
               for (int i = 0; i < arrChiTiet.Count; i++)
               {
                   for (int j = 0; j < MonAn.Count; j++)
                   {
                       if (arrChiTiet[i].MaMonAn == MonAn[j].MaMonAn)
                           MonAn.RemoveAt(j);
                   }

               }
           }
            foreach(var row in MonAn)
            {
                DataGridViewRow _newRow = (DataGridViewRow)gvDSMonAn.Rows[0].Clone();
                _newRow.Cells[0].Value = row.MaMonAn;
                _newRow.Cells[1].Value = row.TenMonAn;
                gvDSMonAn.Rows.Add(_newRow);
            }
    
        }
        int tuanthu = 0;
        int year = DateTime.Now.Year;
        string week = "";
        private void frmLapThucDon_Load(object sender, EventArgs e)
        {
            if (int.Parse(dd[0]) != 0)
            {
                txtTuan.Text = dd[1];
                lbThu.Text = dd[2];
                btnNgayTiep.Visible = false;
                if (dd[3] == "u")
                {
                    DanhSachMonAn_HienTai(int.Parse(dd[0]));
                    LayDS_MonAn();
                    btnLuu.Enabled = true;
                }
                else
                    LayDS_MonAn();
            }
            else
            {
                LayDS_MonAn();
                TuanTrongNam();
            }
            
        }
        public void TuanTrongNam()
        {
            GetWeek tuan = new GetWeek();
            tuanthu = tuan.GetWeekOrderInYear();
            tuanthu = tuanthu + 1;
            if (tuanthu > 52)
            {
                tuanthu++;
                year++;
            }
            week = tuanthu + "_" + year;
            txtTuan.Text = week.ToString();
        }
        private void btnNext_Click(object sender, EventArgs e)
        {
            if (gvDSChon.RowCount >= 1)
            {
                btnLuu.Enabled = true;
            }
            foreach (DataGridViewRow item in this.gvDSMonAn.SelectedRows)
            {
                if(!item.IsNewRow)
                {
                    DataGridViewRow row = gvDSMonAn.Rows[item.Index];
                    DataGridViewRow _newRow = (DataGridViewRow)gvDSChon.Rows[0].Clone();
                    _newRow.Cells[0].Value = int.Parse(row.Cells[0].Value.ToString());
                    _newRow.Cells[1].Value = row.Cells[1].Value.ToString();
                    _newRow.Cells[2].Value = 10;
                    gvDSChon.Rows.Add(_newRow);

                    gvDSMonAn.Rows.Remove(gvDSMonAn.CurrentRow);

                }
            }
            
            
        }

    
        private void gvDSMonAn_CellClick(object sender, DataGridViewCellEventArgs e)
        {
           
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void btnPre_Click(object sender, EventArgs e)
        {
           
            foreach (DataGridViewRow item in this.gvDSChon.SelectedRows)
            {
                if (!item.IsNewRow)
                {
                    DataGridViewRow row = gvDSChon.Rows[item.Index];
                    DataGridViewRow _newRow = (DataGridViewRow)gvDSMonAn.Rows[0].Clone();
                    _newRow.Cells[0].Value = int.Parse(row.Cells[0].Value.ToString());
                    _newRow.Cells[1].Value = row.Cells[1].Value.ToString();
                    gvDSMonAn.Rows.Add(_newRow);
                    // gvDSChon.Rows.RemoveAt(item.Index);
                    gvDSChon.Rows.Remove(gvDSChon.CurrentRow);

                }
            }
            if (gvDSChon.RowCount <= 1)
            {
                btnLuu.Enabled = false;
            }
            
        }

        private void gvDSMonAn_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            btnNext_Click(null, null);
        }

        private void gvDSChon_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            btnPre_Click(null, null);
        }
        int ngay = 2;
        private void btnNgayTiep_Click(object sender, EventArgs e)
        {
            if (gvDSChon.RowCount > 1)
            {
                DialogResult dialogResult = MessageBox.Show("Danh Sách Chưa lưu, Bạn Thật Sự  Muốn Qua Ngày Tiếp Theo?", "Cảnh Báo!", MessageBoxButtons.YesNo);
                if (dialogResult == DialogResult.Yes)
                {
                    ChuyenNgay();
                }
                else if (dialogResult == DialogResult.No)
                {
                    return;
                }
            }
            else
                ChuyenNgay();
        }

        public void ChuyenNgay()
        {
            ngay++;
            btnLuu.Enabled = false;
            //btnNgayTiep.Enabled = false;
            if (ngay == 8)
            {
                lbThu.Text = "Chủ Nhật";
                btnNgayTiep.Enabled = false;
            }
            else
                lbThu.Text = "Thứ " + ngay;

            gvDSMonAn.Rows.Clear();
            LayDS_MonAn();
            gvDSChon.Rows.Clear();
            //string t = gvDSChon.RowCount.ToString();
            // MessageBox.Show(t);
            result = false;
        }
        private bool temp = false;
        public bool kiemtrathucdontuan()
        {
            LapThucDonBus busDS = new LapThucDonBus();
            List<ThucDonPublic> arrthucdon = new List<ThucDonPublic>();
            arrthucdon = busDS.NgayTrongTuan(txtTuan.Text.Trim());
            if (arrthucdon.Count > 0)
            {
                MessageBox.Show("Danh Sách Thực Đơn Tuần Này Đã Được Lập, Xin Chọn Chức Năng Quản Lý Thực Đơn Để Kiểm Tra!", "Thông Báo!");
                return false;
            }
            return true;
            
        }
        private void btnLuu_Click(object sender, EventArgs e)
        {
            if((int.Parse(dd[0]) != 0))
            {
                if (dd[3] == "u")
                {
                    LapThucDonBus busDT = new LapThucDonBus();
                    busDT.XoaChiTiet(int.Parse(dd[0]));
                }
                
                ThemCT_ThucDon(int.Parse(dd[0]));
                btnLuu.Enabled = false;
                gvDSMonAn.Enabled = false;
                MessageBox.Show("Thành Công!");
            }
            else
            {
                if (!temp)
                {
                    temp = kiemtrathucdontuan();
                }
                if (temp)
                {
                    if (!result)
                    {
                        ThemThucDon();
                    }
                    if (result)
                    {
                        ThemCT_ThucDon(ThucDon.MaThucDon);
                    }
                    btnLuu.Enabled = false;
                    // btnNgayTiep.Enabled = true;
                    gvDSMonAn.Rows.Clear();
                    LayDS_MonAn();
                    gvDSChon.Rows.Clear();
                    if (ngay >= 8)
                    {
                        MessageBox.Show("Hoàn Thành Lập Thực Đơn");
                        btnNgayTiep.Enabled = false;
                        gvDSMonAn.Enabled = false;
                    }
                    else
                        btnNgayTiep_Click(null, null);
                }
            }
            
        }
        THUCDON_NGAY ThucDon = new THUCDON_NGAY();
        bool result = false;
        public void ThemThucDon()
        {
            LapThucDonBus busTD = new LapThucDonBus();
            ThucDon.Ngay = Convert.ToDateTime(dtpNagyLap.Text);
            ThucDon.NgayTrongTuan = lbThu.Text;
            ThucDon.Tuan = txtTuan.Text;
            result = busTD.ThemThucDon(ThucDon);

        }
        public void ThemCT_ThucDon(int maThucDon)
        {
            if (result || (int.Parse(dd[0]) != 0))
            {
                LapThucDonBus busCTTD = new LapThucDonBus();
                for (int i = 0; i < this.gvDSChon.RowCount-1; i++)
                {
                     
                    CHITIET_THUCDON CT = new CHITIET_THUCDON();
                    CT.MaThucDon = maThucDon;
                    CT.MaMonAn = int.Parse(gvDSChon.Rows[i].Cells[0].Value.ToString());
                    CT.SoPhanAn = int.Parse(gvDSChon.Rows[i].Cells[2].Value.ToString());
                    busCTTD.CapNhatChiTiet(CT);
                }
            }
        }
    }
}
